package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理页面")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @ApiOperation("新增一个菜品")
    @PostMapping
    public Result addNewDish(@RequestBody DishDTO dishDTO) {
        log.info("新增一个菜品：{}", dishDTO);

        dishService.addNewDish(dishDTO);

        return Result.success();
    }


    @ApiOperation("菜品按页查询")
    @GetMapping("/page")
    public Result<Object> selectDishByPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品按页查询：{}", dishPageQueryDTO);


//        List<> result = dishService.selectDishByPage(dishPageQueryDTO);
        PageResult r = dishService.selectDishByPage(dishPageQueryDTO);
        return Result.success(r);
    }


    @ApiOperation("修改一个菜品")
    @PutMapping
    public Result updateOneDish(@RequestBody DishDTO dishDTO) {
        log.info("修改一个菜品：{}", dishDTO);

        dishService.updateOneDish(dishDTO);

        return Result.success();
    }


    @ApiOperation("菜品查询回显")
    @GetMapping("/{id}")
    public Result<Object> selectDishById(@PathVariable Long id) {
        log.info("菜品查询回显：{}", id);

        DishDTO dishDTO = dishService.selectOneDish(id);


        return Result.success(dishDTO);
    }


    @ApiOperation("菜品批量删除")
    @DeleteMapping
    public Result deleteDishById(@RequestParam List<Integer> ids) {
        log.info("菜品批量删除：{}", ids);

        dishService.deleteDishById(ids);

        return Result.success();
    }


    @ApiOperation("菜品起售停售")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("菜品起售停售，当前操作菜品为：{}", id);

        dishService.updateStatus(status, id);

        return Result.success();
    }


    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<Object> selectDishByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品：{}", categoryId);

        List<DishDTO> dishDTOList = dishService.selectDishByCategoryId(categoryId);

        return Result.success(dishDTOList);
    }
}
